package com.lianmai.chaincard.utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;


import com.lianmai.chaincard.MyApplication;

import java.io.File;

/**
 * SD卡
 */
public class SDCardStore {
    protected File rootDir;
    private File phoneCameraDir;
    private File vivoCameraDir;

    public SDCardStore() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            rootDir = new File(Environment.getExternalStorageDirectory(), "ChainCard");
            vivoCameraDir = new File(Environment.getExternalStorageDirectory(), "名片");
            phoneCameraDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            if (phoneCameraDir != null) {
                phoneCameraDir = new File(phoneCameraDir, "Chain");
            }
            if (!phoneCameraDir.exists()) {
                phoneCameraDir.mkdirs();
            }
            if (!rootDir.exists()) {
                rootDir.mkdirs();
            }
        }
    }

    @SuppressLint("StaticFieldLeak")
    public void checkSaveVideo(final String videoPath) {
        if (vivoCameraDir == null || videoPath == null) {
            return;
        }
        File source = new File(videoPath);
        if (!source.exists()) {
            return;
        }
        if (AppConfig.DEVICE_MODEL != null && AppConfig.DEVICE_MODEL.toLowerCase().contains("vivo")) {
            final String dstPath = vivoCameraDir.getAbsolutePath() + File.separator + source.getName();
            new AsyncTask<Void, Void, String>() {
                @Override
                protected String doInBackground(Void... params) {
                    FileUtil.copy(videoPath, dstPath);
                    return dstPath;
                }

                @Override
                protected void onPostExecute(String s) {
                    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    scanIntent.setData(Uri.fromFile(new File(s)));
                    MyApplication.getInstance().sendBroadcast(scanIntent);
                }
            }.executeOnExecutor(MyApplication.threadPool);
        }
    }

    /**
     * 获取SD卡图片存放路径
     *
     * @return 文件夹路径
     */
    public String getCameraDirPath() {
        if (phoneCameraDir != null) {
            return phoneCameraDir.getAbsolutePath();
        }
        File file = new File(rootDir, "chain");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 获取SD卡缓存路径
     *
     * @return 文件夹路径
     */
    public String getTempDirPath() {
        File file = new File(rootDir, "temp");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 获取下载文件夹路径
     *
     * @return 文件夹路径
     */
    public String getDownloadDirPath() {
        File file = new File(rootDir, "download");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 获取SD卡录像存放路径
     *
     * @return 文件夹路径
     */
    public String getRecorderPath() {
        File file = new File(rootDir, "recorder");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.getAbsolutePath();
    }
}
